package com.example.project;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.text.method.KeyListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
/**

 */
	private int LoopCurrentIP = 0;
	ListView ls;
	ToggleButton b;
	private ProgressDialog pd;
	private ArrayList<String> emp_list;
	WifiManager wifi;
	WifiInfo info;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		b=(ToggleButton) findViewById(R.id.toggleButton1);
		ls=(ListView)findViewById(R.id.listView1);
		b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				info=wifi.getConnectionInfo();
				int ipAddress=info.getIpAddress();
				String ip = null;

				ip = String.format("%d.%d.%d.%d",
				(ipAddress & 0xff),
				(ipAddress >> 8 & 0xff),
				(ipAddress >> 16 & 0xff),
				(ipAddress >> 24 & 0xff));
				
				
				Toast.makeText(getApplicationContext(),ip, Toast.LENGTH_SHORT).show();
				ArrayList<InetAddress> a=getConnectedDevices(ip);
				
			}
		});
		wifi=(WifiManager) getSystemService(WIFI_SERVICE);
		
	}
	
		
	
	public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
	    ArrayList<InetAddress> ret = new ArrayList<InetAddress>();

	    LoopCurrentIP = 0;

	    String IPAddress = "";
	    String[] myIPArray = YourPhoneIPAddress.split("\\.");
	    InetAddress currentPingAddr;

	    for (int i = 0; i <= 255; i++) {
	        try {

	            // build the next IP address
	            currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
	                    myIPArray[1] + "." +
	                    myIPArray[2] + "." +
	                    Integer.toString(LoopCurrentIP));

	            // 50ms Timeout for the "ping"
	            if (currentPingAddr.isReachable(50)) {

	                ret.add(currentPingAddr);
	            }
	        } catch (UnknownHostException ex) {
	        } catch (IOException ex) {
	        }

	        LoopCurrentIP++;
	    }
	    return ret;
	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}






















/*
public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
       // Log.e(LOG_TAG, ex.toString());
    }
    return null;
}



private ArrayList<String> scanSubNet(String subnet){
    ArrayList<String> hosts = new ArrayList<String>();

    InetAddress inetAddress = null;
    for(int i=1; i<10; i++){
       // Log.d(TAG, "Trying: " + subnet + String.valueOf(i));
        try {
            inetAddress = InetAddress.getByName(subnet + String.valueOf(i));
            if(inetAddress.isReachable(1000)){
                hosts.add(inetAddress.getHostName());
               // Log.d(TAG, inetAddress.getHostName());
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return hosts;
}
*/
